package eu.reply.flashmob

import grails.converters.JSON
import org.springframework.dao.DataIntegrityViolationException

class RestController {

    def listEvents() { 
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
	    render  Event.list(params) as JSON
	}
	
	def listProfiles() {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		render  UserProfile.list(params) as JSON
	}
	
	def showEvent() 
	{
		render Event.get(params.id) as JSON
		
	}
	
	def registerProfile() 
	{
		def username = params.name
		def deviceId = params.regId
		
		def userProfile = UserProfile.findByName(username)
		if (!userProfile) {
			userProfile = new UserProfile()	
			userProfile.name = username
		} 
		userProfile.registrationId = deviceId
		def result
		if (!userProfile.save(flush: true)) 
		{
			result = [result: 'KO']	
		}
		result = [result: 'OK']
		render result as JSON
	}
	
	def showProfile() {
		render UserProfile.get(params.id) as JSON
	}
	
	def saveEvent() {
       def eventInstance = new Event(params)
	   def result
        if (!eventInstance.save(flush: true)) {
			result = [result: 'KO']
           
        } else {
			result = [result: 'OK']
		}

		render result as JSON
    }
	
	
	def updateEvent() {
		 def eventInstance = Event.get(params.id)
		 def result 
        if (!eventInstance) {
			result = [result: 'KO']
            render result as JSON
		
        }


        eventInstance.properties = params

        if (!eventInstance.save(flush: true)) {
			result = [result: 'KO']
            render result as JSON
        } else {
			result = [result: 'OK']
			render result as JSON
		}

	}

	def acceptEvent() 
	{
		def eventName = params.eventName
		def profileName = params.profileName
		
		def userProfile = UserProfile.findByName(profileName)
		def event = Event.findByName(eventName)
		
		def result 
		
		if (!userProfile || !event) {
			result = [result: 'KO']
			render result as JSON	
		} else {
			event.attendees.add(userProfile)
	
			if (!event.save(flush: true)) {
				result = [result: 'KO']
			} else {
				result = [result: 'OK']
			}
			
			render result as JSON
		}
	
	}
	
	def saveProfile() {
		def userProfileInstance = new UserProfile(params)
		def result
		if (!userProfileInstance.save(flush: true)) {
			result = [result: 'KO']
			render result as JSON
		}
		result = [result: 'OK']
		render  result as JSON
	}
	
	def updateProfile() {
		def userProfileInstance = UserProfile.get(params.id)
		def result 
		if (!userProfileInstance) {
			result = [result: 'KO'] 
			render result as JSON
		}

		

		userProfileInstance.properties = params

		if (!userProfileInstance.save(flush: true)) {
			result = [result: 'KO']
			render result as JSON
		} else {
			result = [result: 'OK']
			render result as JSON
		}

		
	}

	def deleteProfile() {
		def userProfileInstance = UserProfile.get(params.id)
		def result 
		if (!userProfileInstance) {
			result = [result: 'KO']
			render result as JSON
		}

		try {
			userProfileInstance.delete(flush: true)
			result = [result: 'OK']
			render result as JSON
		}
		catch (DataIntegrityViolationException e) {
			result = [result: 'KO']
			render result as JSON
		}
	}
	
	def deleteEvent() {
	 def eventInstance = Event.get(params.id)
     def result   
	 if (!eventInstance) {
		   result = [result: 'KO']
           render result as JSON
        }

        try {
            eventInstance.delete(flush: true)
			result = [result: 'OK']
			render  result as JSON
        }
        catch (DataIntegrityViolationException e) {
			result = [result: 'KO']
			render result as JSON
        }
	}
}
